﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JKF.BusinessLayer.BLL;
using JKF.Substructure;

namespace JKF.Web.Sales.Nutrition
{
    public partial class Sales_Nutrition_Add : PageBase
    {
        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            MODULE_NAME_EN = "SALES_NUTRITION";
        }
        #endregion

        #region Manager
        public bllPRODUCT productMgr = new bllPRODUCT();
        public bllSALES_NUTRITION nutritionMgr = new bllSALES_NUTRITION();
        public bllCUSTOMER customerMgr = new bllCUSTOMER();
        public bllSYS_USER userMgr = new bllSYS_USER();
        private StringBuilder stringBuilder = new StringBuilder();
        #endregion

        #region DataSource
        /// <summary>
        /// 返回营养系列
        /// </summary>
        public string NutritionProductOptions
        {
            get
            {
                var products = productMgr.GetListArray(
                    string.Format("[type]='3' and (shop_code in(select shop_code from sys_user_shop where user_key='{0}') or shop_code is null)",
                    bllSYS_USER.CurrentUser.USER_KEY.ToString()));
                stringBuilder.Clear();
                foreach (var product in products)
                {
                    stringBuilder.AppendFormat("<option value=\"{0}\" model=\"{1}\" price=\"{3}\">{2}</option>",
                        HtmlEncode(product.PRODUCT_KEY.ToString()),
                        HtmlEncode(product.MODEL),
                        HtmlEncode(product.NAME),
                        product.PURCHASE_PRICE
                    );
                }

                return stringBuilder.ToString();
            }
        }

        /// <summary>
        /// 营养系列销售记录
        /// </summary>
        public string NutritionKey
        {
            get { return P("nutritionKey"); }
        }

        /// <summary>
        /// 营养系列销售记录
        /// </summary>
        public string SalesNutritionInfo
        {
            get
            {
                if (!NutritionKey.IsGuidB()) return "null";
                var nutritionInfo = nutritionMgr.GetModel(NutritionKey.ToGuid());
                var customerInfo = customerMgr.GetModel(nutritionInfo.CUSTOMER_KEY);
                var productInfo = productMgr.GetModel(nutritionInfo.PRODUCT_KEY);
                var sysUser = userMgr.GetModel(nutritionInfo.USER_CREATE);
                return new
                    {
                        NUTRITION_KEY = nutritionInfo.NUTRITION_KEY.ToString(),
                        UNIQUE_CODE = nutritionInfo.UNIQUE_CODE,
                        CARD_NO = customerInfo.CARD_NO,
                        CUSTOMER_KEY = customerInfo.CUSTOMER_KEY.ToString(),
                        CUSTOMER_NAME = customerInfo.NAME,
                        MOBILE = customerInfo.MOBILE,
                        PHONE = customerInfo.PHONE,
                        PRODUCT_KEY = nutritionInfo.PRODUCT_KEY,
                        PRODUCT_NAME = productInfo.NAME,
                        PRODUCT_MODEL = productInfo.MODEL,
                        NUMS = nutritionInfo.NUMS.ToStringNoValue(),
                        PRICE = nutritionInfo.PRICE.ToStringNoValue(),
                        MONEY = nutritionInfo.MONEY.ToStringNoValue(),
                        ADD_POINT = nutritionInfo.ADD_POINT.ToStringNoValue(),
                        STATUS_NAME = nutritionInfo.STATUS == "1" ? "有效" : "无效",
                        OPERATOR = sysUser.REAL_NAME,
                        MEMO = nutritionInfo.MEMO,
                        PURCHASE_DATE = nutritionInfo.PURCHASE_DATE.ToShortDateLineFormat(),
                        DATE_CREATE = nutritionInfo.DATE_CREATE.ToShortDateLineFormat()

                    }.ToJson();
            }
        }

        public string PointRate
        {
            get
            {
                var paramVl = bllSYS_PARAM.GetPointRate();
                return string.IsNullOrEmpty(paramVl) ? "1" : paramVl;
            }
        }
        #endregion
    }
}